const exp=require('express')
const router=exp.Router()
const vierflyUser=require('../middlewares/virefly')
const newsMdel=require('../db/model/newsModel')


router.get('/',(req,res)=>{
  res.redirect('/list')
})
router.get('/list',(req,res)=>{
  let username=req.session.username||null
  newsMdel.find((err,data)=>{
    // console.log(data);
    res.render('list.html',{data,username})
  })
})
router.get('/addnews',vierflyUser,(req,res)=>{
  res.render('addnews.html')
})
router.get('/update',vierflyUser,(req,res)=>{
  let {id} = req.query;

  newsMdel.findById(id,(err,data)=>{
        res.render('update.html',{data})
    })
})
router.get('/delnews/:id',vierflyUser,(req,res)=>{
  let {id}=req.params
  newsMdel.deleteOne({_id:id},(err,data)=>{
    res.redirect('/list')
  })
})
router.get('/detail/:id',(req,res)=>{
  let {id} = req.params;
  newsMdel.findById(id,(err,data)=>{
        res.render('detail.html',{data})
    })
})
router.get('/user',vierflyUser,(req,res)=>{
  res.render('user.html')
})

module.exports=router